//
//  UserSettingViewController.m
//  MovieDate
//
//  Created by ann on 16/3/11.
//  Copyright © 2016年 MovieDate. All rights reserved.
//

#import "UserSettingViewController.h"
#import "ChangePassWordViewController.h"
#import "LogViewController.h"
#import "NSString+ClearCache.h"
#import "AppDelegate.h"
@interface UserSettingViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (strong, nonatomic) IBOutlet UIView *pview;

@property (weak, nonatomic) IBOutlet UILabel *changePassWordLB;
@property (weak, nonatomic) IBOutlet UIButton *messageBTN;
@property (weak, nonatomic) IBOutlet UIButton *likeAndAttentionBTN;
@property (weak, nonatomic) IBOutlet UIButton *vibrationBTN;
@property (weak, nonatomic) IBOutlet UIButton *showGirlBTN;
@property (weak, nonatomic) IBOutlet UIButton *showBoyBTN;
@property (weak, nonatomic) IBOutlet UILabel *clearCache;
@property (weak, nonatomic) IBOutlet UILabel *cacheShowLabel;


@property (weak, nonatomic) IBOutlet UILabel *exitAccountNumberLB;

@end

@implementation UserSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self initView];
    // Do any additional setup after loading the view from its nib.
}
-(void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBarHidden = NO;
    self.tabBarController.tabBar.hidden = YES;
}

-(void)initView {
   
    //_pview.frame = self.view.frame;
    [self.pTableView setTableHeaderView:_pview];

   // [self.pTableView setSectionHeaderHeight:self.view.frame.size.height];
    self.pTableView.delegate = self;
    
    self.pTableView.dataSource = self;
    
    UIView *view = [[UIView alloc]init];
    [self.pTableView setTableFooterView:view];


    //修改密码事件
     self.changePassWordLB.userInteractionEnabled = YES;
    UITapGestureRecognizer *changePSWtap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapChangePassWord:)];
    [self.changePassWordLB addGestureRecognizer:changePSWtap];
    
    //清除缓存
    UITapGestureRecognizer *clearCacheTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapclearCache:)];
    self.clearCache.userInteractionEnabled = YES;
    self.cacheShowLabel.userInteractionEnabled = YES;
    [self.clearCache addGestureRecognizer:clearCacheTap];
    [self.cacheShowLabel addGestureRecognizer:clearCacheTap];
    
    
   //设置导航栏
    [self selDefaultNavigationBackground];
    [self selDefaultNavigationWithTitle:@"设置"];
    [self selAddLeftBackButton];
  
    // 退出当前账号
    UITapGestureRecognizer *exitTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapexitAccountNumber:)];
    self.exitAccountNumberLB.userInteractionEnabled = YES;
    [self.exitAccountNumberLB addGestureRecognizer:exitTap];

    //消息开关
    [self.messageBTN addTarget:self action:@selector(messageAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.messageBTN setSelected:[[NSUserDefaults standardUserDefaults] boolForKey:MACROS_KEY_DISAGREE_UMPUSH]];
    //喜欢和关注开关
    [self.likeAndAttentionBTN addTarget:self action:@selector(likeAndAttentionAction:) forControlEvents:(UIControlEventTouchUpInside)];
     [self.likeAndAttentionBTN  setSelected: YES];
    //应用程序振动开关
    [self.vibrationBTN addTarget:self action:@selector(vibrationAction:) forControlEvents:(UIControlEventTouchUpInside)];
     [self.vibrationBTN  setSelected: YES];
    //为我显示美女开关
    [self.showGirlBTN addTarget:self action:@selector(showGirlAction:) forControlEvents:(UIControlEventTouchUpInside)];
  
    //为我显示帅哥开关
    [self.showBoyBTN addTarget:self action:@selector(showBoyAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    switch ([[[NSUserDefaults standardUserDefaults] valueForKey:kUserShowBoyOrGirl] intValue]) {
        case 1:
            [self.showGirlBTN  setSelected:YES];
            [self.showBoyBTN  setSelected:NO];
            
            break;
        case 2:
            [self.showGirlBTN  setSelected: NO];
            [self.showBoyBTN  setSelected: YES];

            break;
        case 3:
            [self.showGirlBTN  setSelected: NO];
            [self.showBoyBTN  setSelected: NO];

            break;
    }
    float folder =   [self folderSizeAtPath:[self getCachesPath]];
   
  self.cacheShowLabel.text =    [NSString stringWithFormat:@"%.2fM",folder];

    
}
-(NSString *)getCachesPath{
    // 获取Caches目录路径
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *cachesDir = [paths objectAtIndex:0];
    
    NSString *filePath = [cachesDir stringByAppendingPathComponent:@"Caches"];
    
    return filePath;
}
-(float)fileSizeAtPath:(NSString *)path{
    NSFileManager *fileManager=[NSFileManager defaultManager];
    if([fileManager fileExistsAtPath:path]){
        long long size=[fileManager attributesOfItemAtPath:path error:nil].fileSize;
        return size/1024.0/1024.0;
    }
    return 0;
}
-(float)folderSizeAtPath:(NSString *)path{
    NSFileManager *fileManager=[NSFileManager defaultManager];
    float folderSize;
    if ([fileManager fileExistsAtPath:path]) {
        NSArray *childerFiles=[fileManager subpathsAtPath:path];
        for (NSString *fileName in childerFiles) {
            NSString *absolutePath=[path stringByAppendingPathComponent:fileName];
            folderSize +=[self fileSizeAtPath:absolutePath];
        }
        
        //SDWebImage框架自身计算缓存的实现
        folderSize+=[[SDImageCache sharedImageCache] getSize]/1024.0/1024.0;
        return folderSize;
    }
    return 0;
}
-(void)tapclearCache:(UITapGestureRecognizer *)tap {
    
    [self clearCacheAction];
    
    float folder =   [self folderSizeAtPath:[self getCachesPath]];
    
    self.cacheShowLabel.text =    [NSString stringWithFormat:@"%.2fM",folder];

}

//清除缓存
-(void)clearCacheAction{
    NSFileManager *fileManager=[NSFileManager defaultManager];
    NSString * path = [NSHomeDirectory() stringByAppendingString:@"/Library/Caches"];
    if ([fileManager fileExistsAtPath:path]) {
        
        NSArray *childerFiles=[fileManager subpathsAtPath:path];
        
        for (NSString *fileName in childerFiles) {
            
            //如有需要，加入条件，过滤掉不想删除的文件
            NSString *absolutePath = [path stringByAppendingPathComponent:fileName];
            [fileManager removeItemAtPath:absolutePath error:nil];
       }
       }
}

//退出当前账号事件
-(void)tapexitAccountNumber:(UITapGestureRecognizer *)tap{

    [[NSUserDefaults standardUserDefaults] setValue:@"" forKey:@"myToken"];
    UIStoryboard *story = [UIStoryboard storyboardWithName:@"Login" bundle:nil];
    AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
    UINavigationController *nav = [story instantiateInitialViewController];
    appDelegate.window.rootViewController =  nav;
}

//修改密码事件
-(void)tapChangePassWord:(UITapGestureRecognizer *)tapGR{
    ChangePassWordViewController *changePSW = [[ChangePassWordViewController alloc]init];
    [self.navigationController pushViewController:changePSW animated:YES];
    
}

#warning 推送消息功能待完成事件
//消息事件
- (void)messageAction:(UIButton *)btn {
    btn.selected = !btn.selected;
    
    UIApplication *app = [UIApplication sharedApplication];
    AppDelegate *appDelegate = app.delegate;
    [[NSUserDefaults standardUserDefaults] setBool:btn.selected forKey:MACROS_KEY_DISAGREE_UMPUSH];
    [appDelegate selCloseNoti];
}

#warning 喜欢和关注待完成事件
//喜欢和关注开关事件
-(void)likeAndAttentionAction:(UIButton *)btn{
    
    [self changeBackgroundImageBTN:btn];
}
#warning 振动开关功能待完成事件
 //应用程序振动开关事件
-(void)vibrationAction:(UIButton *)btn{
     [self changeBackgroundImageBTN:btn];
    
}

//为我显示美女开关事件
- (void)showGirlAction:(UIButton *)btn{
    btn.selected = !btn.selected;
    [self selUserState];
}
  //为我显示帅哥开关事件
- (void)showBoyAction:(UIButton *)btn{
    btn.selected = !btn.selected;
    [self selUserState];
}

- (void)selUserState
{
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    if (self.showGirlBTN.selected && !self.showBoyBTN.selected) {
        [userDefault setValue:@"1" forKey:kUserShowBoyOrGirl];
    } else if (!self.showGirlBTN.selected && self.showBoyBTN.selected) {
        [userDefault setValue:@"2" forKey:kUserShowBoyOrGirl];
    } else {
        [userDefault setValue:@"3" forKey:kUserShowBoyOrGirl];
    }
    [userDefault synchronize];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadSeeList" object:nil];
}

//切换按钮图片
-(void)changeBackgroundImageBTN:(UIButton *)btn{
    btn.selected=!btn.selected;//每次点击都改变按钮的状态
    if (btn.selected) {
        [btn setBackgroundImage:[UIImage imageNamed:@"switch_on"] forState:(UIControlStateNormal)];
        [btn setBackgroundImage:[UIImage imageNamed:@"switch_on"] forState:(UIControlStateHighlighted)];
    }else{
        [btn setBackgroundImage:[UIImage imageNamed:@"switch_off"] forState:(UIControlStateNormal)];
        [btn setBackgroundImage:[UIImage imageNamed:@"switch_off"] forState:(UIControlStateHighlighted)];
    }
}


#pragma mark  -UITableViewDelegate,UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 0;

}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:identifier];
    }
    return cell;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    self.pTableView.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-40);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
